import { createFromIconfontCN } from '@ant-design/icons';
import { iconFontUrl } from '@/services/urlconst';
import './styles.less';

const IconFont = createFromIconfontCN({
  scriptUrl: iconFontUrl(), //图标在线[本地使用，测试环境打包]
  // scriptUrl: '/fonts/iconfont.js', //图标离线处理[UAT环境打包，生产环境打包]
});

const MyIcon = ({
  className,
  type,
  showType = 'js',
  style,
  isTheme = true, //判断图标库 颜色 是否随主题变化 而改变
  onClick,
}: {
  type: Icon.IconType;
  showType?: string;
  style?: Icon.IconStyle;
  isTheme?: boolean;
  className?: string;
  onClick?: () => void;
}) => {
  let t = type;
  if (showType === 'js' && typeof t === 'string' && t.indexOf('icon-') === 0) {
    t = type;
  } else if (showType === 'js' && typeof t === 'string') {
    t = 'icon-' + type;
  }

  return (
    <span className={isTheme ? 'one-icon' : ''}>
      {showType === 'js' ? (
        <IconFont onClick={onClick} className={className} type={t} style={{ ...style }} />
      ) : showType === 'css' ? (
        <i style={{ ...style }}>{type}</i>
      ) : null}
    </span>
  );
};

export default MyIcon;
